/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bibSysTray;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author Pardal
 */
public class BibsysTray {

    /* Again singleton design pattern so we don't need to keep any reference 
     * on the tray icon. Useful to display messages.
     */
    private static BibsysTray trayIcon;
    private SystemTray tray;
    private static TrayIcon icon;

    public BibsysTray() {
        if (SystemTray.isSupported()) {
            tray = SystemTray.getSystemTray();
            Image bibSysIcon = new ImageIcon(BibsysTray.class.getResource("book_icon.gif"), "BibSys").getImage();

            PopupMenu menu = new PopupMenu();

            MenuItem item1 = new MenuItem("BibSys");
            item1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    bibsys.BibSys.showWindow();
                }
            });
            menu.add(item1);

            MenuItem item2 = new MenuItem("Sair");
            item2.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
            menu.add(item2);


            icon = new TrayIcon(bibSysIcon, "BibSys", menu);
            icon.setImageAutoSize(true);
            try {
                tray.add(icon);
            } catch (AWTException ex) {
                Logger.getLogger(BibsysTray.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void showMessage(String title, String message) {
        if (icon != null) {
            icon.displayMessage(title, message, TrayIcon.MessageType.INFO);
        }
    }
}
